package com.kxmall.huishou.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kxmall.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.kxmall.huishou.domain.bo.HsWasteActivityBo;
import com.kxmall.huishou.domain.vo.HsWasteActivityVo;
import com.kxmall.huishou.domain.HsWasteActivity;
import com.kxmall.huishou.mapper.HsWasteActivityMapper;
import com.kxmall.huishou.service.IHsWasteActivityService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 废品回收活动息Service业务层处理
 *
 * @author kxmall
 * @date 2023-11-23
 */
@RequiredArgsConstructor
@Service
public class HsWasteActivityServiceImpl implements IHsWasteActivityService {

    private final HsWasteActivityMapper baseMapper;

    /**
     * 查询废品回收活动息
     */
    @Override
    public HsWasteActivityVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询废品回收活动息列表
     */
    @Override
    public TableDataInfo<HsWasteActivityVo> queryPageList(HsWasteActivityBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HsWasteActivity> lqw = buildQueryWrapper(bo);
        Page<HsWasteActivityVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询废品回收活动息列表
     */
    @Override
    public List<HsWasteActivityVo> queryList(HsWasteActivityBo bo) {
        LambdaQueryWrapper<HsWasteActivity> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HsWasteActivity> buildQueryWrapper(HsWasteActivityBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HsWasteActivity> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), HsWasteActivity::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getOrganizer()), HsWasteActivity::getOrganizer, bo.getOrganizer());
        lqw.eq(StringUtils.isNotBlank(bo.getContactor()), HsWasteActivity::getContactor, bo.getContactor());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), HsWasteActivity::getPhone, bo.getPhone());
        lqw.eq(bo.getStartTime() != null, HsWasteActivity::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, HsWasteActivity::getEndTime, bo.getEndTime());
        lqw.eq(bo.getStatus() != null, HsWasteActivity::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增废品回收活动息
     */
    @Override
    public Boolean insertByBo(HsWasteActivityBo bo) {
        HsWasteActivity add = BeanUtil.toBean(bo, HsWasteActivity.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改废品回收活动息
     */
    @Override
    public Boolean updateByBo(HsWasteActivityBo bo) {
        HsWasteActivity update = BeanUtil.toBean(bo, HsWasteActivity.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HsWasteActivity entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除废品回收活动息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
//        HsWasteActivity hsWasteActivity = new HsWasteActivity();
//        hsWasteActivity.setDelFlag(1l);
//        int num = baseMapper.update(hsWasteActivity, new QueryWrapper<HsWasteActivity>().in("id",ids));
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
